﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace ConnectLib.DAO
{
    public class ServiceDao
    {
        private string _serviceid;
        private string _name;
        private int _price;
        private string _imagelink;
        private string _summary;
        private string _fullcontent;
        private byte _active;
        private byte _sort;
        private byte _hightsort;

        public string ServiceId
        {
            get { return _serviceid; }
            set { _serviceid = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public int Price
        {
            get { return _price; }
            set { _price = value; }
        }

        public string ImageLink
        {
            get { return _imagelink; }
            set { _imagelink = value; }
        }

        public string Summary
        {
            get { return _summary; }
            set { _summary = value; }
        }

        public string FullContent
        {
            get { return _fullcontent; }
            set { _fullcontent = value; }
        }

        public byte Active
        {
            get { return _active; }
            set { _active = value; }
        }

        public byte Sort
        {
            get { return _sort; }
            set { _sort = value; }
        }

        public byte HighSort
        {
            get { return _hightsort; }
            set { _hightsort = value; }
        }

        public void InitEmptyValue()
        {
            _serviceid = string.Empty;
            _name = string.Empty;
            _price = 0;
            _imagelink = string.Empty;
            _summary = string.Empty;
            _fullcontent = string.Empty;
            _active = 0;
            _sort = 0;
            _hightsort = 0;
        }

        public ServiceDao()
        {
            InitEmptyValue();
        }

        public ServiceDao(Hashtable hsObj)
        {
            InitEmptyValue();
            try
            {
                PropertyInfo[] arrProInfo = this.GetType().GetProperties();
                PropertyInfo proInfo;
                object objValue;
                string sValue;
                string proName;
                for (int i = 0; i < arrProInfo.Length; i++)
                {
                    proInfo = arrProInfo[i];
                    proName = proInfo.Name.ToLower();
                    if (hsObj.ContainsKey(proName))
                    {
                        objValue = hsObj[proName];
                        if (proInfo.PropertyType.Name.ToLower() == "byte[]")
                        {
                            if (objValue != null && objValue != DBNull.Value)
                            {
                                proInfo.SetValue(this, (byte[]) objValue, null);
                            }
                        }
                        else
                        {
                            sValue = Convert.ToString(objValue);
                            if (string.IsNullOrEmpty(sValue))
                                continue;
                            switch (proInfo.PropertyType.Name.ToLower())
                            {
                                case "byte":
                                    proInfo.SetValue(this, Convert.ToByte(objValue), null);
                                    break;
                                case "int64":
                                    proInfo.SetValue(this, Convert.ToInt64(objValue), null);
                                    break;
                                case "int32":
                                    proInfo.SetValue(this, Convert.ToInt32(objValue), null);
                                    break;
                                case "datetime":
                                    proInfo.SetValue(this, Convert.ToDateTime(objValue), null);
                                    break;
                                case "byte[]":
                                    break;
                                default:
                                    proInfo.SetValue(this, objValue, null);
                                    break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
